package de.koller.reason.gui;

import de.koller.reason.RSea;
import de.koller.reason.ReasonStart;
import de.koller.reason.gui.component.mo.Glower;
import de.koller.reason.gui.renderObj.IRenderObject;
import de.koller.reason.gui.renderObj.IRenderPosition;
import de.koller.reason.gui.renderObj.IVolatileRenderObject;
import org.lwjgl.opengl.GL11;

public class RenderRoot extends RSea {

	public final Glower glower = new Glower();
	
	@Override
	protected void init() {
		super.init();
		GL11.glMatrixMode( GL11.GL_PROJECTION );
		GL11.glLoadIdentity();

		GL11.glOrtho( 0, ReasonStart.WIDTH, ReasonStart.HEIGHT, 0, -1000, 1000 );
		
		GL11.glMatrixMode( GL11.GL_MODELVIEW );
		GL11.glLoadIdentity();
		
		GL11.glEnable( GL11.GL_CULL_FACE );
		GL11.glCullFace( GL11.GL_BACK );

		GL11.glEnable( GL11.GL_DEPTH_TEST );
		GL11.glDepthFunc( GL11.GL_LEQUAL );
		
		GL11.glAlphaFunc( GL11.GL_GREATER, 0.1f );
		GL11.glEnable( GL11.GL_ALPHA_TEST );
		
		glower.create();
	}
	
	public final void renderSzene() {
		GL11.glClear( GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT );
		GL11.glColor3f( 1, 1, 1 );
		
		for( IRenderListener r : this.rroot.guiRoot.renderListeners )
			r.renderThis();
		
		this.rroot.guiRoot.getBasicTexture().bind();
		for( IRenderObject r : this.rroot.guiRoot.basicRenderObjs ) 
			renderIRO( r );
		
		this.rroot.guiRoot.staticGlyph.getScreen().getTexture().bind();
		this.rroot.guiRoot.staticGlyph.getScreen().updateBuffer();
		for( IRenderObject r : this.rroot.guiRoot.staticGlyphRenderObj ) 
			renderIRO( r );
		
		this.rroot.guiRoot.volatileGlyph.getScreen().getTexture().bind();
		for( IRenderObject vro : rroot.guiRoot.volatileGlyphRenderObj )
			((IVolatileRenderObject)vro).renderFrame();
		this.rroot.guiRoot.volatileGlyph.getScreen().updateBuffer();
		for( IRenderObject r : this.rroot.guiRoot.volatileGlyphRenderObj ) 
			renderIRO( r );
		
		glower.renderGlows();
	}
	
	private void renderIRO( IRenderObject r ) {
		IRenderPosition p = r.getRenderPos();
		this.rroot.guiRoot.getBasicTexture().draw(	p.getX(), p.getY(), 
													p.getWidth(), p.getHeight(),
													p.getZ(), r.getSubPart() );
	}
	
}
